package com.wuxiaoqin.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxiaoqin.server.entity.Commodity;
import com.wuxiaoqin.server.entity.Goodsimg;
import com.wuxiaoqin.server.entity.Goodsstock;
import com.wuxiaoqin.server.mapper.ClothessizeMapper;
import com.wuxiaoqin.server.mapper.CommodityMapper;
import com.wuxiaoqin.server.mapper.GoodsimgMapper;
import com.wuxiaoqin.server.mapper.GoodsstockMapper;
import com.wuxiaoqin.server.service.ICommodityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-10-28
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements ICommodityService {

    @Autowired(required = false)
    private CommodityMapper commodityMapper;
    @Autowired
    private ClothessizeMapper clothessizeMapper;
    @Autowired
    private GoodsstockMapper goodsstockMapper;

    @Autowired
    private GoodsimgMapper goodsimgMapper;


    public QueryWrapper<Commodity> queryUtils(){
        QueryWrapper<Commodity> cqw = new QueryWrapper<Commodity>().eq("C_state",0);
        //排除创建时间和创建状态两个字段
        cqw.select(Commodity.class, info -> !info.getColumn().equals("C_createtime")
                && !info.getColumn().equals("C_state")
                && !info.getColumn().equals("C_firstClass")
                && !info.getColumn().equals("C_secondClass")
                && !info.getColumn().equals("C_size")
                && !info.getColumn().equals("C_salesVolumes")
        );

        return cqw;
    }



    /**
     * 查询高销量商品
     * @param number
     * @return
     */
    @Override
    public List<Commodity> goodsBySale(Integer number) {

        List<Commodity> goodsBySaleList = commodityMapper.getGoodsBySale(number);
        return goodsBySaleList;
    }

    @Override
    public List<Commodity> getHotGood(Integer fristmenu,Integer number) {
        List<Commodity> hotGood = commodityMapper.getHotGood(fristmenu, number);
        return hotGood;
    }

    /**
     * 分页所有商品
     * @param pn
     * @param size
     * @return
     */
    @Override
    public Page<Commodity> getAllGoods(Integer pn, Integer size) {

/*        QueryWrapper<Commodity> cqw = new QueryWrapper<>();
        //排除创建时间和创建状态两个字段
        cqw.select(Commodity.class, info -> !info.getColumn().equals("C_createtime")
                && !info.getColumn().equals("C_state"))
                .orderByDesc("C_salesVolumes");*/

        Page<Commodity> commodityPage = new Page<>(pn,size);

        return commodityMapper.selectPage(commodityPage, queryUtils());
    }

    @Override
    public Commodity getGoodById(Integer id) {
        QueryWrapper<Commodity> c_id = queryUtils().eq("C_id", id).eq("C_state",0);

        Commodity commodity = commodityMapper.selectOne(c_id);
        return commodity;
    }

    @Override
    public Page<Commodity> exploreClassGoods(Integer pn, Integer size, Integer firstClass, Integer secondClass, BigDecimal maxPrice, BigDecimal minPrice) {
        QueryWrapper<Commodity> list = queryUtils()
                .eq("C_firstClass", firstClass).eq("C_secondClass", secondClass)
                .ge("C_price", minPrice).le("C_price", maxPrice)
                .eq("C_state",0);
        Page<Commodity> commodityPage = new Page<>(pn,size);
        Page<Commodity> page = commodityMapper.selectPage(commodityPage, list);
        return page;
    }

    /**
     * 尺码和库存
     * @param id
     * @return
     */
    @Override
    public Goodsstock getGoodsStockById(Integer id) {
        Goodsstock goodsstock = goodsstockMapper.selectById(id);
        return goodsstock;
    }

    /**
     * 查询附加图片
     * @param id
     * @return
     */
    @Override
    public Goodsimg getGoodsImgById(Integer id) {
        Goodsimg goodsimg = goodsimgMapper.selectById(id);
        return goodsimg;
    }


}
